--load systems needed for this state
require 'systems.cleanupSystem'
require 'systems.cameraSystem'
require 'systems.physicsSystem'
--require 'systems.bindSystem'
require 'systems.updateAnimations'
require 'systems.audioSystem'
require 'systems.ai_trackPlayer'
require 'systems.fireWeapon'
require 'systems.onDeathSystem'
require 'worldCollisions'
require 'onDeathFunctions'
require 'systems.powerUpSystem'
require 'systems.disableSystem'
require 'systems.HUDrenderer'

--load renderers
require 'systems.backgroundRenderer'
require 'systems.shapeRenderer'
require 'systems.foregroundRenderer'

mainGame = {}

local sector

function mainGame:init()
end

function mainGame:enter()
  gameScore = 0
  
    player = EntitySystem.entity.player(0,0,100,100, love.graphics.newImage('assets/textures/ship1Anim/anim.png'), love.graphics.newImage('assets/textures/ship1Anim/turboAnim.png'), 90, 25, 1000, 2, 0.1, "DefaultFlying") 
  
  level = EntitySystem.entity.level() 
  
  for i = -1, -4, -1 do
    for j = -7, 7 do
        EntitySystem.entity.sector(i,j, true, 1)      
    end
  end
  
  sector = 4
  
  camera = Cam.new()
  camera:zoomTo(game.window.scale.x*0.7)
  --camera:zoomTo(0.1)
  
  --Physics set up
  love.physics.setMeter(50)
  world = love.physics.newWorld(0,0,true)
  world:setCallbacks(beginContact)
end


function mainGame:update(dt)
  EntitySystem.update(dt)  
  
  
  --Find what sector the player is in
  player.sector.x = math.multiple(player.position.x, 1950)/1950
  player.sector.y = math.multiple(player.position.y, 1050)/1050
  
  --Create new sectors
  if player.sector.x == sector - 1 then
    for j = -10, 10 do
        EntitySystem.entity.sector(-1*(sector + 1),j, true, sector)      
    end
    sector = sector + 1
    gameScore = gameScore + 1
  end

  if player.physics then
      player.physics.body:setLinearVelocity(math.cos(player.physics.body:getAngle())*player.movement.speed, math.sin(player.physics.body:getAngle())*player.movement.speed)
  end
  
  if player.health.value <= 0 then
    player.onDeath.needsRun = true
  end
  
  
end

function mainGame:draw(dt)

  camera:attach()
  EntitySystem.draw(dt)
  camera:detach()  
  
end

function mainGame:leave()
end

--Input
function mainGame:keypressed(k)
  if k == "w" or k == "up" then
    if player.physics then
      player.physics.body:setAngularVelocity(-5)
    end
  elseif k == "s" or k == "down" then
    if player.physics then
      player.physics.body:setAngularVelocity(5)
    end
  end
  
  if k == ' ' then
    player.reload.isFiring = true
  end
  
  --[[if k == "lshift" then
    player.movement.turbo = true
  end]]
  
  if k == "right" or k == "d" then
    player.weapons.selection = 1
    player.reload.speed = 0.2
  elseif k == "left" or k == "a" then
    player.weapons.selection = 2
    player.reload.speed = 1
  end
  
  if k == "escape" then
    if player.health.value > 0  then
      local screenshot = love.graphics.newScreenshot( )
      game.screenshot = love.graphics.newImage(screenshot)
      Gamestate.push(pauseMenu)
    else
          --Delete Everything
    for e in pairs(EntitySystem.query("simpleObject")) do
      EntitySystem.delete(e)
    end
    for e in pairs(EntitySystem.query("physical")) do
      EntitySystem.delete(e)
    end
    for e in pairs(EntitySystem.query("camFollows")) do
      EntitySystem.delete(e)
    end
    for e in pairs(EntitySystem.query("texturedObject")) do
      EntitySystem.delete(e)
    end
    for e in pairs(EntitySystem.query("animatedObject")) do
      EntitySystem.delete(e)
    end
    for e in pairs(EntitySystem.query("soundEffect")) do
      EntitySystem.delete(e)
    end
    for e in pairs(EntitySystem.query("hud")) do
      EntitySystem.delete(e)
    end
    
    --Save high score
    if gameScore > highScore then
      highScore = gameScore
      love.filesystem.write("high.score", highScore)
    end
    
    --Go to the Main Menu
    Gamestate.switch(mainMenu)
    end
    
  end
  
end

function mainGame:keyreleased(k)
  if k == "w" or k == "up" then
    if player.physics then
      player.physics.body:setAngularVelocity(0)
    end
  elseif k == "s" or k == "down" then
    if player.physics then
      player.physics.body:setAngularVelocity(0)
    end
  end
  
  if k == " " then
    player.reload.isFiring = false
  end
  
  --[[if k == "lshift" then
    player.movement.turbo = false
  end]]
  
end

function mainGame:mousepressed(x,y,b)
end
function mainGame:mousereleased(x,y,b)
end

